package pacman.entries.pacman.meta_graph.features;

import pacman.entries.pacman.meta_graph.IMetaGraphFeature;
import pacman.entries.pacman.meta_graph.IMetaGraphObject;
import pacman.entries.pacman.meta_graph.MetaGraphNode;
import pacman.game.Constants.GHOST;
import pacman.game.Game;
import pacman.game.Constants.DM;
import pacman.game.Constants.MOVE;

public class NodeFeatureGhostInTheWay implements IMetaGraphFeature {

	private final float PENALTY = 45.0f;
	
	@Override
	public float Evaluate(IMetaGraphObject object, Game game) {
		
		MetaGraphNode node = (MetaGraphNode)object;
		int index = game.getPacmanCurrentNodeIndex();
		MOVE moveTowardNode = game.getNextMoveTowardsTarget(index, node.getIndex(), DM.PATH);
		double distToNode = game.getDistance(index, node.MazeIndex(), DM.PATH);
		
		float score = 0.0f;
		
		for (GHOST ghost : GHOST.values()) {
			
			// don't be scared of an edible ghost
			if (game.getGhostEdibleTime(ghost) > 0.0f)
				continue;
			
			int ghostIndex = game.getGhostCurrentNodeIndex(ghost);
			
			// if ghost is in other direction, skip it
			MOVE moveTowardGhost = game.getNextMoveTowardsTarget(index, ghostIndex, DM.PATH);
			if (moveTowardGhost != moveTowardNode)
				continue;
			
			// if ghost is past node, skip it
			double distToGhost = game.getDistance(index, ghostIndex, DM.PATH);
			if (distToNode < distToGhost)
				continue;
			
			// if ghost not moving toward pacman, skip it
			MOVE moveFromGhost = game.getNextMoveTowardsTarget(ghostIndex, index, DM.PATH);
			MOVE ghostLastMove = game.getGhostLastMoveMade(ghost);
			if (moveFromGhost != ghostLastMove)
				continue;
			
			score -= PENALTY;
		}
		
		return score;
	}

}
